<?php
namespace Admin\Controller;
use Think\Controller;
use Common\Controller\BaseController;

/**
 * 个人日志管理 
 */

class LogController extends BaseController {
	/*获取日志列表*/
	public function index(){
		$psize = 12;
		// $psize = I('get.psize', $this->m_psize,'intval');
		$s_content = I('get.content','','trim');
		$s_title = I('get.title','','trim');
		$s_type = I('get.type',-1, 'intval');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 12 )
			$psize = $this->m_psize;

		$MLog = D("Log");
		$wheres = array();
		$wheres['_string'] = '1=1';
		$wheres['is_deleted'] = array('neq', 1);
		$wheres['creator_id'] = array('eq', $this->m_user['user_id']);
		if( $s_type != -1)
			$wheres['type'] = array('eq', $s_type );
		if( $s_title )
			$wheres['title'] = array('like','%'.$s_title.'%');
		if( $s_content)
			$wheres['content'] = array('like','%'.$s_content.'%');
		$cnt = $MLog->where( $wheres )->count();

		$Page = new \Common\ORG\Page( $cnt, $psize );

		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$dlist = $MLog->field("log_id,type,content,title,create_time,update_time,audit_res,audit_time,audit_msg,audit_id")
					  ->where( $wheres )
					  ->order("create_time desc ")
					  ->limit($Page->firstRow.','.$Page->listRows )
					  ->select();
		if( $dlist ){
			$types = $MLog->getTypes();
			$_types = array();
			foreach( $types as $v ){
				$_types[$v['k']] = $v['v'];
			}
			foreach( $dlist as &$vd){
				$vd['typename'] = isset( $_types[$vd['type']]) ?  $_types[$vd['type']] : $vd['type'];
			}
			$res['rcode'] = 1;
			$res['data'] = $dlist;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;

		}else{
			$res['rcode'] = 2;
			$res['msg'] = "日志内容为空";
		}
		e_json( $res );
	}

	/*添加日志*/
	public function add(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MLog = D('Log');
		$title = I('post.title',false,'trim');
		$content = I('post.content',false,'trim');
		$type = I('post.type',0,'intval');
		$addres = $MLog->add_log( $title, $content, $type);
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	/*编辑日志*/
	public function edit(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MLog = D('Log');
		$log_id = I('post.log_id',false,'intval');
		$title = I('post.title',false,'trim');
		$content = I('post.contents',false,'trim');
		$type = I('post.type',false,'intval');
		$editArr = array();
		$editArr['log_id'] = $log_id;
		$editArr['title'] = $title;
		$editArr['content'] = $content;
		$editArr['type'] = $type;
		$addres = $MLog->update_log($editArr);
		if( $addres['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $addres['d'];
		}
		else{
			$res['msg'] = $addres['i'];
		}
		e_json( $res );
	}

	/*删除日志*/
	public function remove(){
		$id = I('get.id',false,'intval');
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		if( !$id ){
			$res['msg'] = L('PARAM_WRONG');
		}
		else{
			$MLog = D('Log');
			$delres = $MLog->delete_log( $id );	
			if( $delres['r']){
				$res['rcode'] = 1;
				$res['msg'] = L('DATA_DELETE_OK');
			}else{
				$res['msg'] = $delres['i'];
			}
		}
		

		e_json( $res );
	}
	
	/*获取日志类型*/
	public function logtypes(){
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$MLog = D('Log');
		$res['data'] = $MLog->getTypes();
		e_json( $res );
	}

	/*审核日志列表*/
	public function reviewlist(){
		$psize = 12;
		// $psize = I('get.psize', $this->m_psize,'intval');
		$s_nickname = I('get.nickname','','trim');
		$s_title = I('get.title','','trim');
		$s_type = I('get.type',-1, 'intval');
		$s_state = I('get.state',-1, 'intval');

		if( !$psize )
			$psize = $this->m_psize;
		else if( $psize > 12 )
			$psize = $this->m_psize;

		$MLog = M("Log");
		$DLog = D("Log");
		$MEsctor = M("Sector");
		$wheres = array();
		
		$wheres['log.is_deleted'] = array('neq', 1);
		$wheres['role.is_deleted'] = array('neq', 1);
		$wheres['user.is_deleted'] = array('neq', 1);

		if( $s_type != -1)
			$wheres['log.type'] = array('eq', $s_type );
		if($s_state != -1 && $s_state == 1)	 //待审核日志
			$wheres['log.audit_res'] = array('eq', 0);
		else if($s_state == 2)				//已审核日志：审核通过+审核失败日志
			$wheres['log.audit_res'] = array('in', '1,2');
		if( $s_title )
			$wheres['log.title'] = array('like','%'.$s_title.'%');
		if( $s_nickname)
			$wheres['user.nickname'] = array('like','%'.$s_nickname.'%');
			//可以审核直属下级日志
		$wheres['role.parent_role'] = array('eq',$this->m_user['role_id']);
		if($this->m_user['user_id'] == 1){		 //超级管理员可以查看所有日志
			$wheres['role.parent_role'] = array('EXP','IS NOT NULL');
		}
		$cnt = $MLog->join("role on log.creator_role=role.role_id")
					->join("user on user.user_id=log.creator_id")->where( $wheres )->count();
		$Page = new \Common\ORG\Page( $cnt, $psize );
		
		$res = array('rcode'=>1,'data'=>NULL, 'msg'=> 'OK');
		$dlist1 = $MLog->field("log.log_id,log.type,log.content,log.title,log.audit_res,log.create_time,log.update_time,role.role_name,role.parent_role,
								log.audit_res,log.audit_msg,sector.sector_id,user.nickname,sector.sector_name,sector.parent_sector,log.audit_time,log.audit_id")
				      ->join("sector on sector.sector_id=log.creator_sector")
				      ->join("role on log.creator_role=role.role_id")
				      ->join("user on user.user_id=log.creator_id")
					  ->where( $wheres )
					  ->order("log.create_time desc ")
					  ->limit($Page->firstRow.','.$Page->listRows )
					  ->select();
		$sql = $MLog -> getLastSql();
		if( $dlist1 ){
			$types = $DLog->getTypes();
			$_types = array();
			foreach( $types as $v ){
				$_types[$v['k']] = $v['v'];
			}
			foreach( $dlist1 as &$vd){
				$vd['typename'] = isset( $_types[$vd['type']]) ?  $_types[$vd['type']] : $vd['type'];
			}
			$res['rcode'] = 1;
			$res['data'] = $dlist1;
			$res['pall'] =  $Page->getTotalPages();
			$res['p'] = $Page->nowPage;
			$res['psize'] = $psize;
			$res['cnt'] = $cnt;
			$res['where'] = $wheres;
		}else{
				$res['rcode'] = 2;
				$res['msg'] = "没有可以审核的日志";
				$res['where'] = $wheres;
				$res['map'] = $map;
		}
		e_json($res);
	}

	/*获取审核人信息*/
    public function getUname(){
    	$user_id = I('get.user_id',false,'intval');
    	$DUser = D("User");
    	$wheres['is_deleted'] = array('neq', 1);
    	$wheres['user_id'] = array('eq',$user_id);
    	$uname = $DUser->field('user_id,nickname')->where($wheres)->find();
    	e_json($uname);
    }
    
	//审核日志通过
	public function reviewok(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MLog = D('Log');
		$log_id = I('get.log_id',false,'intval');
		$audit_msg = I('get.audit_msg',"",'trim');
		$resu = $MLog -> audit_logok($log_id,$audit_msg);
		if( $resu['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $resu['d'];
		}
		else{
			$res['msg'] = $resu['i'];
		}
		e_json( $res );
	}

	//审核日志不通过
	public function reviewno(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MLog = D('Log');
		$log_id = I('get.log_id',false,'intval');
		$audit_msg = I('get.audit_msg',"",'trim');
		$resu = $MLog -> audit_logno($log_id,$audit_msg);
		if( $resu['r'] ){
			$res['rcode'] = 1;
			$res['msg'] = 'OK';
			$res['data'] = $resu['d'];
		}
		else{
			$res['msg'] = $resu['i'];
		}
		e_json( $res );
	}

	//获取审核人信息
	public function getaudit(){
		$res = array('rcode'=>0,'data'=>NULL, 'msg'=> 'OK');
		$MUser = D('User');
		$user_id = I('get.id',false,'intval');
		$wheres = array();
		$wheres['is_deleted'] = array('neq', 1);
		$wheres['user_id'] = array('eq',$user_id);
		$user_info = $MUser ->where($wheres) -> find();
		$sql = $MUser ->getLastSql();
		if($user_info){
			$res['rcode'] = 1;
			$res['user_id'] = $user_id;
			$res['data'] = $user_info;
		}else{
			$res['rcode'] = 2;
			$res['msg'] = '没有该审核人';
			$res['user_id'] = $user_id;
		}
		e_json($res);
	}

}